# -*- coding: UTF-8 -*-
"""
用来生成特征

"""
import sys
import time

from datetime import datetime, timezone

from db.feature_processor import FeatureMaker
import traceback


def feature_gen_loop(feature, window, begin_utctime_str, ip, test=False):
    """
    生成特征
    :param feature:
    :param window:
    :param begin_utctime_str:
    :param ip:
    :param test:
    :return:
    """
    feature_maker = FeatureMaker(ip)
    begin_timestamp = int(
        datetime.strptime(begin_utctime_str, '%Y-%m-%d %H:%M:%S').replace(tzinfo=timezone.utc).timestamp())
    item_end_timestamp = begin_timestamp - (window * 60)
    interval = window * 60

    now_timestamp = int(time.time())
    while item_end_timestamp < (now_timestamp - interval):
        try:
            item_end_timestamp += interval
            item_end_utc_str = str(datetime.utcfromtimestamp(item_end_timestamp))
            item_end_local_str = str(datetime.fromtimestamp(item_end_timestamp))

            now_timestamp = int(time.time())
            now_utc_str = str(datetime.utcfromtimestamp(now_timestamp))
            now_local_str = str(datetime.fromtimestamp(now_timestamp))

            print('now time          utc: %s, local: %s, timestamp: %d' % (now_utc_str, now_local_str, now_timestamp))
            print('item end value of utc: %s, local: %s, timestamp: %d' % (
                item_end_utc_str, item_end_local_str, item_end_timestamp))
            print('')
            sys.stdout.flush()

            if not test:
                if feature == 'block':
                    feature_maker.make_block_feature(item_end_timestamp, window)
                elif feature == 'predict_signal':
                    feature_maker.make_return_volatility(item_end_timestamp, window)
        except Exception as e:
            traceback.print_exc()

    while True:
        try:
            now_timestamp = int(time.time())
            if now_timestamp % interval == 0:
                utc_str = str(datetime.utcfromtimestamp(now_timestamp))
                local_str = str(datetime.fromtimestamp(now_timestamp))
                print('now timestamp: %d, utc: %s, local: %s' % (now_timestamp, utc_str, local_str))
                sys.stdout.flush()

                if not test:
                    if feature == 'block':
                        feature_maker.make_block_feature(now_timestamp, window)
                    elif feature == 'predict_signal':
                        feature_maker.make_return_volatility(now_timestamp, window)
            time.sleep(1)
        except Exception as e:
            traceback.print_exc()


if __name__ == '__main__':
    if len(sys.argv) < 4:
        print('[%s] feature_name[block] window_size[15|30|60] begin_utc_time_str[2018-05-31 23:00:00]')
        print(
            'example: ./feature_task.py block 15 "2018-05-31 23:00:00" ')
        exit()
    feature_name = sys.argv[1]
    window_size = int(sys.argv[2])
    begin_utctime_str = sys.argv[3]
    print("input args, feature_name: %s, window_size: %s, begin_utctime_str:%s" % (
        feature_name, window_size, begin_utctime_str))
    feature_gen_loop(feature=feature_name, window=window_size, begin_utctime_str=begin_utctime_str, ip='0.0.0.0',
                     test=False)
